import Store from '@/store/store'
import {getUserInfo} from '@/api/user.js'

Store.registerModule('menu', {
    namespaced: true,
    state: {
        menu: [],
        tabs: [],
        activeItem: '',
        token: '',
        id: '',      //登录用户id
        fromPage: '',     // 
        fromPageIccid: '',    //
        nickName:'',
        cardState:'',//卡状态
        cardDataLevel:''//卡片流量阈值
    },
    getters: {
        subMenu(state) {
            var subs = state.menu.filter(f => f.sub).map(f => f.sub);
            function jsonUniq(arrjson, key) {
                    let arr1 = [arrjson[0]];
                    arrjson.forEach(function (item1, idx1) {
                        let flag = false;
                        arr1.forEach(function (item2, idx2) {
                            if (item1[key] == item2[key]) {
                                flag = true;
                                return;
                            }
                        })
                        if (!flag ) {
                            arr1.push(item1)
                        }
                    })
                    return arr1;
            }
            if(subs && subs.length > 0){
                return jsonUniq(subs,'id');
            }else{
                return [];
            }
        },
        itemMenu(state) {
            return (pid, group) => {
                var items = state.menu
                    .filter(f => f.pid == pid)
                    .map(f => {
                        return {
                            name: f.name,
                            index: f.index,
                            path: f.path
                        }
                    });
                    if(items.length == 5 && items[0].path.includes('sim')){//代理商物联卡管理模块排序
                        var newItems = [];
                        for(var i = 0;i < items.length;i++){
                            if(items[i].name == '物联卡列表' || items[i].name == '卡片管理'){
                                newItems[0] = items[i]
                            }
                            if(items[i].name == '物联卡分销'){
                                newItems[1] = items[i]
                            }
                            if(items[i].name == '到期物联卡' || items[i].name == '到期卡管理'){
                                newItems[2] = items[i]
                            }
                            if(items[i].name == '卡片详情'){
                                newItems[3] = items[i]
                            }
                        }
                        items = newItems
                    }
                return Array.from(new Set(items))
            }
        },
        getNickName(state){
            return state.nickName;
        }
    },
    mutations: {
        initMenu(state, menu) {
            state.menu = menu;
        },
        initTabs(state, tabs) {
            state.tabs = tabs;
        },
        addTab(state, tab) {
            state.tabs.push(tab)
        },
        switchTab(state, nowIndex) {
            state.activeItem = nowIndex
        },
        setNickName(state,nickName){
            state.nickName = nickName;
        },
        setFromPage(state, fromPage){
            state.fromPage = fromPage;
        },
        setFromPageIccid(state, iccid){
            state.fromPageIccid = iccid;
        },
        setToken(state, token){
            state.token = token;
        },
        setId(state, id){
            state.id = id
        },
        setCardState(state, cardState){
            state.cardState = cardState
        },
        setCardDataLevel(state, cardDataLevel){
            state.cardDataLevel = cardDataLevel
        },

    },
    actions: {
        setNickName(context,nickName){
            context.commit('setNickName', nickName);
        },
        getMenu(context) {

            var data = {
                id: localStorage.getItem('id')
            }
            
            getUserInfo(data).then(rsp=>{
                let menus = [];
                if(rsp.data){
					// 设置代理 Id
                    localStorage.setItem('agentId', rsp.data.data.agentId);
					// 遍历每个整个菜单列表
                    rsp.data.data.userMenus.forEach(item => {
                        if(item.bodyMenus && item.bodyMenus.length > 0){
                            item.bodyMenus.forEach(item2=>{

                                // if(item.path == 'home'){
                                //     //forEach循环不支持continue;
                                //     return true;
                                // }else{
                                    menus.push({
                                        name:item2.name,
                                        sub:{'name':item.name,'icon':item.icon,'id':item.id},
                                        index:item2.id,
                                        id:item2.id,
                                        pid:item2.pid,
                                        path: item2.path,
                                        componentPath: 'views/'+ item2.path
                                    });
                                // }
                            });
                        }

                    });
                    context.commit('initMenu', menus);
                }
            });
        },
        clickMenuItem(context, index) {
            if (index != "adminIndex") {
                var tab = context.state.tabs.find(f => f.index == index)
                if (!tab) {
                    let menu = context.state.menu.find(f => f.index == index)
                    let component =import(`@/${menu.componentPath ? menu.componentPath : 'components/NotFound.vue'}`)
                    
                    var newTab = {
                        label: menu.name,
                        index: menu.index,
                        closable: true,
                        component:menu.componentPath
                    }

                    context.commit('addTab', newTab)
                }
            }
            context.commit('switchTab', index)
        },
        closeTab(context, index) {
            let indexNum = context.state.tabs.findIndex(f => f.index == index);
            let activeItem = context.state.activeItem;
            let newTabs = context.state.tabs.filter(f => f.index != index);
            context.commit('initTabs', newTabs)
            if (activeItem == index) {
                context.commit('switchTab', indexNum == 0 ? "adminIndex" : newTabs[indexNum - 1].index)
            }
        }
    }
})